"""
@author: 江同学呀
@file: completion_field.py
@date: 2024/7/27 23:52
@desc:
    https://www.elastic.co/guide/en/elasticsearch/reference/7.17/search-suggesters.html#completion-suggester

    Used for auto-complete suggestions.
    用于自动完成建议。
"""
from abc import ABC
from typing import Type, Union, Dict

from espc.orm.model.base.base import _Base
from espc.orm.model.mapping.field.base_field.base_string_field import _BaseStringField
from espc.orm.model.mapping.field.field_attribute import (
    AnalyzerAttribute, SearchAnalyzerAttribute, PreserveSeparatorsAttribute, PreservePositionIncrementsAttribute,
    MaxInputLengthAttribute
)
from espc.utils.build_add_attr import build_add_attr


class CompletionField(
    _BaseStringField, AnalyzerAttribute, SearchAnalyzerAttribute, PreserveSeparatorsAttribute,
    PreservePositionIncrementsAttribute, MaxInputLengthAttribute, ABC
):
    """
    完成 字段
    """
    _field_type: str = "completion"

    def __init__(
            self, field_name: str = None, value: str = None, default: str = None, analyzer: str = None,
            search_analyzer: str = None, preserve_separators: Union[str, bool] = None,
            preserve_position_increments: Union[str, bool] = None, max_input_length: int = None,
    ):
        super(CompletionField, self).__init__(
            field_name=field_name, value=value, default=default, analyzer=analyzer, search_analyzer=search_analyzer,
            preserve_separators=preserve_separators, preserve_position_increments=preserve_position_increments,
            max_input_length=max_input_length,
        )
        return

    def __get__(self, instance: _Base, owner: Type["CompletionField"]) -> Union["CompletionField", str]:
        return super(CompletionField, self).__get__(instance=instance, owner=owner)

    def __set__(self, instance, value: str):
        super(CompletionField, self).__set__(instance=instance, value=value)
        return

    def __getattribute__(self, item):
        return super(CompletionField, self).__getattribute__(item)

    @build_add_attr
    def _build(self) -> Dict:
        mapping: Dict = {
            "type": self._field_type
        }
        return mapping





